package dbPicasso::UI::Dialog::dlgGetObjectSpec;

use base qw(dbPicasso::UI::Dialog);

our $DIALOG;
our $DB_CONN;

sub show
{
    my $class = shift;
    $DB_CONN = shift;

    Gtk2::Rc->parse_string(
        'style "liststyle" { GtkComboBoxEntry::appears-as-list = 1 }
				 widget "*.LoadObjectName" style "liststyle"');

    $DIALOG = $class->SUPER::show("dlgGetObjectSpec", __PACKAGE__, undef, \&init_dialog);
}

sub init_dialog
{
    my ($dlgref, $dlgdef) = @_;

    $DIALOG  = $dlgdef;
    $DB_CONN = $dbPicasso::UI::DB_CONN;
}

sub on_LoadObjectType_changed
{
    my $typesel   = $DIALOG->get_widget('LoadObjectType');
    my $objectsel = $DIALOG->get_widget('LoadObjectName');

    my $type = $typesel->get_active_text;

    %typecode = (
                 'Stored Procedure' => 'sproc',
                 'View'             => 'view',
                 'Table'            => 'table',
                );
    dbPicasso::UI->fill_simple_comboboxentry($objectsel, $DB_CONN->get_object_list($typecode{$type}));

    $objectsel->set_sensitive(1);
}

sub on_LoadObjectOK_clicked
{
    my $objectsel = $DIALOG->get_widget('LoadObjectName');
    my $typesel   = $DIALOG->get_widget('LoadObjectType');

    my %typeconv = (
                    'stored procedure' => 'sproc',
                    'table'            => 'table',
                    'view'             => 'view'
                   );

    my $name = $objectsel->child->get_text;
    my $type = $typesel->get_active_text;

    $type = $typeconv{lc $type};

    my $buffer = $dbPicasso::UI::CODE->get_buffer;

    $buffer->begin_not_undoable_action();
    $buffer->set_text($DB_CONN->get_object_ddl($name, $type));
    $buffer->end_not_undoable_action();

    $buffer->set_modified(0);
    $buffer->place_cursor($buffer->get_start_iter());

    $buffer->apply_tag_by_name('source_font', $buffer->get_bounds);
    dbPicasso::UI::_set_current_file(undef);

}
1;
